<template>
  <div class="headers">
    <van-nav-bar
      title="搜索"
      left-arrow
      @click-left="
        $router.push({
          path: '/index',
          query: {
            latitude: latitude,
            longitude: longitude,
            name: name,
            val: '外卖',
          },
        })
      "
    />

    <!-- 头部 -->
    <div class="top1">
      <div class="top11">
        <input
          v-model="input"
          @input="kong"
          class="input"
          type="text"
          value="请输入商家或美食名称"
        />
        <van-button @click="btn1" type="info">提交</van-button>
      </div>
    </div>
    <div class="yiti" v-if="xianshisosuo">
      <div class="lishi"><span class="lishiso">搜索历史</span></div>
      <div class="list1">
        <ul>
          <li @click="tiao(index)" v-for="(item, index) in array" :key="index">
            <div class="xunr">
              <span>{{ item }}</span
              ><span @click="btn2">X</span>
            </div>
          </li>
          <li @click="btn3" class="qingkong">清空搜索历史</li>
        </ul>
      </div>
    </div>
    <div class="yiti" v-if="xianshishangcheng">
      <div class="lishi"><span class="lishiso">商城</span></div>
      <ul>
        <li v-for="item in xuanr" :key="item.id">
          <div class="right">
            <div class="tup">
              <img :src="url + item.image_path" alt="" />
            </div>
            <div class="wenzi">
              <p>{{ item.name }}</p>
              <div class="jiage">
                <span class="jiagehong">月售{{ item.recent_order_num }}单</span>
              </div>
              <div>
                {{ item.float_minimum_order_amount }}元起送/距离{{
                  item.distance
                }}
              </div>
            </div>
          </div>
        </li>
      </ul>
    </div>

    <!-- 底部 -->
    <div class="foot">
      <Bottom :value="val"></Bottom>
    </div>
  </div>
</template>

<script>
import { restaurants } from "../api/citylist";
import Bottom from "../view/bottom.vue";

export default {
  components: { Bottom },
  created() {
    localStorage.setItem("name", JSON.stringify(this.name));
    localStorage.setItem(
      "latitude",
      JSON.stringify(this.$route.query.latitude)
    );
    localStorage.setItem(
      "longitude",
      JSON.stringify(this.$route.query.longitude)
    );

    let a = "23.12908,113.264359";
    restaurants(a, "冰淇淋").then((res) => {
      console.log(res);
    });
    console.log(1);
  },
  data() {
    return {
      url: "https://elm.cangdu.org/img/",
      array: [],
      input: "",
      xianshisosuo: true,
      xianshishangcheng: false,
      xuanr: "",
      val: this.$route.query.val,
      name: JSON.parse(localStorage.getItem("name")),
      longitude: JSON.parse(localStorage.getItem("longitude")),
      latitude: JSON.parse(localStorage.getItem("latitude")),
    };
  },
  methods: {
    tiao(index) {},
    kong() {
      if (this.input == "") {
        this.xianshisosuo = true;
        this.xianshishangcheng = false;
        this.xuanr = "";
      } else {
        this.xianshisosuo = false;
        this.xianshishangcheng = true;
      }
    },
    btn1() {
      if (this.array.indexOf(this.input) == -1) {
        this.array.push(this.input);
        console.log(this.array);
      }

      console.log(this.array);
      let a = "23.12908,113.264359";
      restaurants(a, this.input).then((res) => {
        console.log(res);
        this.xuanr = res.data;
      });
    },
    btn2(index) {
      this.array.splice(index, 1);
    },
    btn3() {
      this.array = [];
    },
  },
};
</script>
<style lang="less" scoped>
/deep/.van-nav-bar__content {
  background-color: #3190e8;
  .van-nav-bar__arrow {
    color: #fff;
  }
  .van-nav-bar__title {
    color: #fff;
  }
}
.closeleft {
  height: 100px;
  background-color: #eeeeee;
  .zongjia {
  }
}
li {
  border-bottom: 1px solid #eeeeee;
}
.jiage {
  margin-bottom: 10px;
  .jiagehong {
    color: red;
  }
  padding: 0 10px;
  margin-top: 10px;
  font-size: 20px;
  display: flex;
  justify-content: space-between;
  .van-stepper {
    width: 120px;
  }
}
.wenzi {
  margin-top: 18px;
  margin-left: 30px;
  font-size: 19px;
  flex: 1;
  p {
    color: #47a5ff;
  }
}
.tup {
  margin-top: 18px;
  height: 60px;
  width: 50px;

  img {
    width: 128%;
    height: 134%;
  }
}
.left {
  display: flex;
}
.right {
  display: flex;
  flex: 1;
  height: 120px;
}
.qingkong {
  font-size: 30px;
  font-weight: 700;
  color: #3190e8;
  height: 85px;
  line-height: 85px;
  text-align: center;
}
.actrey {
  display: block;
}
.yc {
  display: none;
}
.list1 {
  li {
    border-bottom: 1px solid #e6e6e6;
  }
  .xunr {
    padding: 0 20px;
    display: flex;
    height: 60px;
    line-height: 60px;
    font-size: 25px;
    color: #67586a;
    justify-content: space-between;
  }
}
.lishi {
  margin-top: 20px;
  line-height: 60px;
  height: 60px;
  background-color: #f2f2f2;
  .lishiso {
    margin-left: 20px;
    font-size: 20px;
    color: #66667d;
    font-weight: 700;
  }
}
.top1 {
  margin-top: 75px;
  .top11 {
    widows: 80%;
    padding: 0 20px;
    display: flex;
    justify-content: space-between;

    .input {
      border: none;
      background-color: #e6e0e0;
      height: 50px;
      width: 78%;
      border-radius: 10px;

      font-size: 20px;
      color: #66667d;
      font-weight: 700;
    }
    .van-button {
      width: 20%;
      height: 50px;
      border-radius: 6px;
    }
  }
}

.abb {
  position: fixed;
  bottom: 0;
}
.header {
  width: 100%;
  background-color: #3190e8;
  display: flex;
  height: 50px;
  justify-content: space-between;
  align-items: center;
  color: #fff;

  .van-icon {
    font-size: 30px;
  }
}

.foot {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
}

.headers {
  height: 100vh;
}
</style>